package com.example.dao;

import com.example.pojo.Department;
import com.example.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 *
 */
@Repository
public class EmployeeDao {
    private static Map<Integer, Employee> employees = null;
    @Autowired
    private DepartmentDao departmentDao;
    static{
        employees = new HashMap<Integer, Employee>();
        employees.put(1001, new Employee(1001, "ray1", "email1", 0, new Department(101, "教学")));
        employees.put(1002, new Employee(1002, "ray2", "email2", 0, new Department(102, "教学")));
        employees.put(1003, new Employee(1003, "ray3", "email3", 1, new Department(103, "教学")));
        employees.put(1004, new Employee(1004, "ray4", "email4", 0, new Department(104, "教学")));
        employees.put(1005, new Employee(1005, "ray5", "email5", 1, new Department(105, "教学")));
    }
    private static Integer initId = 1006;
    //增加一个员工
    public void save(Employee employee){
        if(employee.getId() == null){
            employee.setId(initId ++);
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(), employee);
    }
    //查询员工信息
    public Collection<Employee> getAll(){
        return employees.values();
    }
    public Employee getEmployeeById(Integer id){
        return employees.get(id);
    }
    public void delete(Integer id){
        employees.remove(id);
    }
}
